package scuba.DiveBuddy.data;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import scuba.DiveBuddy.Class.AirMix;

/**
 * @author Alex Anderson
 * @version 1
 * @since 27/01/14.
 *
 * Containing static methods for accessing and manipulating the UserSettings Table
 */
public class AirMixSource {
    private static final String TABLE_NAME = "AirMix";

    private static final String COLUMN_ID = "ID";
    private static final String COLUMN_NAME = "Name";
    private static final String COLUMN_O2LEVEL = "O2";
    private static final String COLUMN_NLEVEL = "Nitrogen";
    private static final String COLUMN_HLEVEL = "Helium";
    private static final String COLUMN_LOCKED = "Locked";

    //TABLE MOD [#STATICS]
    public static void CreateTable(SQLiteDatabase db) {
        String create = "create table " + TABLE_NAME  +" (" +
                COLUMN_ID + " integer primary key autoincrement," +
                COLUMN_NAME + " varchar(16) not null," +
                COLUMN_O2LEVEL + " integer default 21," +
                COLUMN_NLEVEL + " integer default 78," +
                COLUMN_HLEVEL + " integer default 0," +
                COLUMN_LOCKED + " integer default 0);";

        Log.w("DATABASE", "Creating table " + TABLE_NAME + " with STATEMENT = '" + create + "'");

        try {
            Log.w("DATABASE", "Created table " + TABLE_NAME);
            db.execSQL(create);

        }
        catch(android.database.SQLException sql) {
            Log.e("DATABASE", "Create Failed on " + AirMixSource.TABLE_NAME + " with error = '" + sql + "'");
        }
    }
    public static void DropTable(SQLiteDatabase db) {
        String drop = "drop table if exists " + TABLE_NAME;

        Log.w("DATABASE",
                "Dropping table " + TABLE_NAME + " with STATEMENT = '" + drop + "'");

        try {
            Log.w("DATABASE", "Table " + TABLE_NAME + " Dropped");
            db.execSQL(drop);
        }
        catch(SQLException sql) {
            Log.e("DATABASE", "Failed to drop table " + TABLE_NAME + " with error = '" + sql + "'");
        }
    }
    public static void UpgradeTable(SQLiteDatabase db) {
        DropTable(db);
        CreateTable(db);
    }

    //
    public static AirMix[] GetMixes() {
        AirMix[] mixes;

        SQLiteDatabase db = Database.getInstance().getDatabase("AirMixSource::GetMixes()");
        String query = "SELECT * FROM " + TABLE_NAME;

        Cursor c = db.rawQuery(query, null);
        if(c.getCount() == 0) {
            c.close();
            Log.w("DATABASE", "Query: '" + query + "' returned null");
            return new AirMix[] { }; //nothing
        }

        mixes = new AirMix[c.getCount()];
        c.moveToFirst();

        for(int i =0 ; i < c.getCount(); i++)
        {
            AirMix temp = new AirMix(
                    c.getInt(c.getColumnIndex(COLUMN_ID)),
                    c.getString(c.getColumnIndex(COLUMN_NAME)),
                    c.getFloat(c.getColumnIndex(COLUMN_O2LEVEL)),
                    c.getFloat(c.getColumnIndex(COLUMN_NLEVEL)),
                    c.getFloat(c.getColumnIndex(COLUMN_HLEVEL)),
                    c.getInt(c.getColumnIndex(COLUMN_LOCKED)) != 0);

            mixes[i] = temp;
            c.moveToNext();
        }

        c.close();
        return mixes;
    }
    public static int CreateMix(AirMix mix) {
        SQLiteDatabase db = Database.getInstance().getDatabase("AirMixSource::CreateMix()");

        ContentValues values = getContentValues(mix);
        long id = db.insert(TABLE_NAME, null, values);

        Log.v("DATABASE", "Added Mix {"+ mix.getName() + "}: " + mix.getLongDesc());
        return (int)id;
    }

    private static ContentValues getContentValues(AirMix mix) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME, mix.getName());
        values.put(COLUMN_O2LEVEL, mix.getO2());
        values.put(COLUMN_NLEVEL, mix.getN());
        values.put(COLUMN_HLEVEL, mix.getH());
        values.put(COLUMN_LOCKED, mix.isLocked() ? 1 : 0);
        return values;
    }

    public static boolean UpdateMix(AirMix mix) {
        int rows = Database.getInstance().getDatabase("AirMixSource:UpdateMix()")
                .update(TABLE_NAME, getContentValues(mix), COLUMN_ID + "=" + mix.getID(),
                        null);

        return rows == 1;
    }
    public static boolean LockMix(AirMix mix) {
        if(mix.isLocked())
            return true; //already locked don't care



        return false; //NYI
    }

}
